﻿using System;
using System.Data;
using System.Collections.Generic;
using VeMayBayDTO;

namespace VeMayBayDAO
{
    public  class HanhTrinhDAO
    {
        public static HanhTrinhDTO GetHanhtrinhAll()
        {
            HanhTrinhDTO ht = new HanhTrinhDTO();
            return ht;
        }
        public static HanhTrinhDTO GetHanhTrinh(HanhTrinhDTO temp)
        {
            try
            {
                // lay ma hanh trinh neu da co hanh trinh trong table hanh trinh
                HanhTrinhDTO ht = new HanhTrinhDTO();
                string query = "select * from hanhtrinh where sanbaydi =" + temp.SanBayDi + " and sanbayden =" + temp.SanBayDen + "";
                DataTable dt = new DataTable();
                dt = DataProvider.ExecQuery(query);
                if (dt.Rows.Count > 0)
                {
                    ht.MaHanhTrinh = (int)dt.Rows[0]["mahanhtrinh"];
                    ht.SanBayDen = (int)dt.Rows[0]["sanbayden"];
                    ht.SanBayDi = (int)dt.Rows[0]["sanbaydi"];
                }
                else if (dt.Rows.Count == 0)
                {// neu chua co thi ta insert la lay ma hanh trinh vua moi them vao
                    if (ThemHanhTrinh(temp))
                    {
                        string sql = "select * from hanhtrinh where sanbaydi = " + temp.SanBayDi + " and sanbayden =" + temp.SanBayDen + " ORDER BY mahanhtrinh DESC";
                        dt = DataProvider.ExecQuery(query);
                        ht.MaHanhTrinh = (int)dt.Rows[0]["mahanhtrinh"];
                        ht.SanBayDen = (int)dt.Rows[0]["sanbayden"];
                        ht.SanBayDi = (int)dt.Rows[0]["sanbaydi"];
                    }
                }
                return ht;
            }
            catch (Exception)
            {
                
                throw;
            }
           
           
        }
        public static HanhTrinhDTO GetHanhtrinhById(string id)
        {
            HanhTrinhDTO ht = new HanhTrinhDTO();
            
            return ht;
        }
        public static bool ThemHanhTrinh(HanhTrinhDTO ht)
        {
            string query = "insert into hanhtrinh (sanbayden,sanbaydi) values (" + ht.SanBayDen + "," + ht.SanBayDi + ")";
            int n = (int)DataProvider.ExecNonQuery(query);
            if (n == 0)
                return false;
            return true;
        }
        public static bool HuyHanhTrinh(string id)
        {
            return true;
        }
        public static bool SuaHanhTrinh(HanhTrinhDTO ht)
        {
            return true;
        }
    }
}
